package app.controllers;

import app.models.Debito;
import app.services.DebitoService;
import app.util.DateUtils;
import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.Validator;

@Resource
public class DebitoController {

	private Result result;
	private Validator validator;
	private DebitoService service;
	
	public DebitoController(Result result, Validator validator, DebitoService service){
		this.result = result;
		this.validator = validator;
		this.service = service;
	}
	
	
	@Get("/debitos")
	public void index() {
		this.result.include("debitos", service.findAllOrdered());
		this.result.include("ultimosDebitos", service.findLastDebitos());
	}
	
	
	@Get("/debito/find")
	public void index(String filter) {
		this.result.include("filter", filter);
		this.result.include("debitos", service.findByFilter(filter));
		
		this.result.of(this).table();
	}
	
	
	@Get("/debito/new")
	public void form() { }
	
	
	@Post("/debito/save")
	public void save(Debito debito){		
		validator.validate(debito);
		
		validator.onErrorForwardTo(this).form();
		
		service.create(debito);
		this.result.forwardTo(this).index();
	}
	
	public void table() { }
	
}
